﻿using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.Templet;
using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormAttachment : FormTemplet
    {
        private string _sOrderCode = null;

        private FormOrderInfo _frmOrderInfo = null;

        public FormAttachment()
        {
            this.InitializeComponent();
        }

        public FormAttachment(FormOrderInfo frmOrderInfo, string sOrderCode) : this()
        {
            this._frmOrderInfo = frmOrderInfo;
            this._sOrderCode = sOrderCode;
            this.Text += this._sOrderCode;
            this.RefreshItems();
        }

        private void RefreshItems()
        {
            DataTable dataSource = null;
            Orders orders = new Orders();
            try
            {
                dataSource = orders.GetAttachments(this._sOrderCode);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            orders.Dispose();
            this.dg.DataSource = dataSource;
            this.dg.Columns[0].Visible = false;
        }

        private void FormAttachment_Load(object sender, EventArgs e)
        {
            this.dg.AutoResizeColumns();
        }

        private void btnDownload_Click(object sender, EventArgs e)
        {
            if (this.dg.Rows.Count < 1)
            {
                MessageBox.Show("没有数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                byte[] array = null;
                Orders orders = new Orders();
                try
                {
                    array = orders.LoadAttachment(Convert.ToInt64(this.dg.Rows[this.dg.CurrentRow.Index].Cells[0].Value));
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                orders.Dispose();
                if (array != null)
                {
                    this.saveFileDialog1.Filter = "所有类型(*.*)|*.*";
                    this.saveFileDialog1.FileName = this.dg.Rows[this.dg.CurrentRow.Index].Cells[1].Value.ToString();
                    if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        FileStream fileStream = new FileStream(this.saveFileDialog1.FileName, FileMode.Create);
                        BinaryWriter binaryWriter = new BinaryWriter(fileStream);
                        binaryWriter.Write(array);
                        binaryWriter.Flush();
                        fileStream.Close();
                        binaryWriter.Close();
                    }
                }
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            this.RefreshItems();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.Order_Update])
            {
                MessageBox.Show("你没有权限操作这里！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (this.dg.Rows.Count < 1)
            {
                MessageBox.Show("没有数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (MessageBox.Show("确定要删除吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.No)
            {
                bool flag = false;
                Orders orders = new Orders();
                try
                {
                    flag = orders.DeleteFile(Convert.ToInt64(this.dg.Rows[this.dg.CurrentRow.Index].Cells[0].Value));
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                orders.Dispose();
                if (flag)
                {
                    this.dg.Rows.Remove(this.dg.Rows[this.dg.CurrentRow.Index]);
                    this._frmOrderInfo.RefreshAttachments();
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }
    }
}
